﻿using System;
using System.Globalization;
using System.Runtime.InteropServices;

namespace UFrame
{
    public class DateTool
    {
        public static DateTime standardTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);

        public static string time24Str = "yyyy-MM-dd HH:mm:ss";

        /// <summary>
        /// 获得当前时间 
        /// </summary>
        public static string GetCurDate()
        {
            return DateTime.Now.ToString(time24Str);
        }

        /// <summary>
        /// 获得前一天时间
        /// </summary>
        public static string GetOnTheDay()
        {
            var d = DateTime.Now.AddDays(-1);
            string str = d.ToString(time24Str);
            return str;
        }


        /// <summary>
        /// 获取当前系统时间 毫秒
        /// </summary>
        /// <returns></returns>
        public static string GetCurSysTime_Ms()
        {
            TimeSpan ts = DateTime.UtcNow - standardTime;
            string ret = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            return ret;
        }

        /// <summary>
        /// 当前时间差 毫秒
        /// </summary>
        /// <returns></returns>
        public static string GetCurSysTime_Ms_Diff(string msTime)
        {
            TimeSpan ts = DateTime.UtcNow - standardTime;
            Int64 _msSerTime = Convert.ToInt64(msTime);
            Int64 _msLocalTime = Convert.ToInt64(ts.TotalMilliseconds);
            string ret = "时延 " + (_msLocalTime - _msSerTime).ToString();
            return ret;
        }

        public static long GetToDayTotalSeconds(int hour, int minute)
        {
            var now = DateTime.Now;
            DateTime time = new DateTime(now.Year, now.Month, now.Day, hour, minute, 0, 0);
            long timeStamp = (long) (time - now).TotalSeconds; // 相差秒数
            return timeStamp;
        }

        public static int GetDayOfWeek()
        {
            return (int) DateTime.Now.DayOfWeek;
        }

        /// <summary>
        /// 0 = 毫秒 1 = 秒
        /// </summary>
        /// <param name="unixTimeStamp"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static DateTime UnixToDateTime(long unixTimeStamp, int type = 0)
        {
            DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(standardTime); // 当地时区
            switch (type)
            {
                case 0:
                {
                    return startTime.AddMilliseconds(unixTimeStamp);
                }
                default:
                {
                    return startTime.AddSeconds(unixTimeStamp);
                }
            }
        }

        public static string SecondToDay(int remain)
        {
            TimeSpan ts = new TimeSpan(0, 0, Convert.ToInt32(remain));
            string content = "";

            if (ts.Days > 0)
            {
                content = $"{ts.Days}天{ts.Hours}时{ts.Minutes}分";
            }
            else if (ts.Hours > 0)
            {
                content = $"{ts.Hours}时{ts.Minutes}分";
            }
            else
            {
                content = $"{ts.Minutes}分";
            }

            return content;
        }

        public static string UnixToString(long unixTimeStamp, int type = 0)
        {
            DateTime a = UnixToDateTime(unixTimeStamp, type);
            return a.ToString("yyyy-MM-dd HH:mm:ss");
        }


        public static void SetLocalDate(int unixTime)
        {
            var dt = UnixToDateTime(unixTime, 1);
            SetLocalDate(dt);
        }

        public static void SetLocalDate(string date)
        {
            DateTimeFormatInfo dtFormat = new DateTimeFormatInfo();
            dtFormat.ShortDatePattern = "yyyy-MM-dd hh:mm:ss";
            var dt = Convert.ToDateTime(date, dtFormat);
            SetLocalDate(dt);
        }

        public static void SetLocalDate(DateTime time)
        {
            SystemTime sys = new SystemTime();
            sys.vYear = (ushort) time.Year;
            sys.vMonth = (ushort) time.Month;
            sys.vDay = (ushort) time.Day;
            sys.vHour = (ushort) time.Hour;
            sys.vMinute = (ushort) time.Minute;
            sys.vSecond = (ushort) time.Second;
            SetLocalTime(sys);
        }

        //系统时间
        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(SystemTime st);

        [DllImportAttribute("Kernel32.dll")]
        private static extern void SetLocalTime(SystemTime st);

        [StructLayoutAttribute(LayoutKind.Sequential)]
        public class SystemTime
        {
            public ushort vYear;
            public ushort vMonth;
            public ushort vDayOfWeek;
            public ushort vDay;
            public ushort vHour;
            public ushort vMinute;
            public ushort vSecond;

            public override string ToString()
            {
                string str = "{0}/{1}{2} {3}:{4}:{5}";
                str = string.Format(str, vYear, vMonth, vDay, vHour, vMinute, vSecond);
                return str;
            }
        }
    }
}